package cn.iocoder.yudao.module.system.controller.admin.ward;

import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageItemRespVO;
import cn.iocoder.yudao.module.system.convert.user.UserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.ward.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.ward.WardDO;
import cn.iocoder.yudao.module.system.convert.ward.WardConvert;
import cn.iocoder.yudao.module.system.service.ward.WardService;

@Tag(name = "管理后台 - 系统病区")
@RestController
@RequestMapping("/system/ward")
@Validated
public class WardController {
    @Resource
    private DeptService deptService;
    @Resource
    private WardService wardService;

    @PostMapping("/create")
    @Operation(summary = "创建系统病区")
    @PreAuthorize("@ss.hasPermission('system:ward:create')")
    public CommonResult<Long> createWard(@Valid @RequestBody WardCreateReqVO createReqVO) {
        return success(wardService.createWard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统病区")
    @PreAuthorize("@ss.hasPermission('system:ward:update')")
    public CommonResult<Boolean> updateWard(@Valid @RequestBody WardUpdateReqVO updateReqVO) {
        wardService.updateWard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统病区")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:ward:delete')")
    public CommonResult<Boolean> deleteWard(@RequestParam("id") Long id) {
        wardService.deleteWard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统病区")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:ward:query')")
    public CommonResult<WardRespVO> getWard(@RequestParam("id") Long id) {
        WardDO ward = wardService.getWard(id);
        return success(WardConvert.INSTANCE.convert(ward));
    }

    @GetMapping("/list")
    @Operation(summary = "获得系统病区列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:ward:query')")
    public CommonResult<List<WardRespVO>> getWardList(@RequestParam("ids") Collection<Long> ids) {
        List<WardDO> list = wardService.getWardList(ids);
        return success(WardConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统病区分页")
    @PreAuthorize("@ss.hasPermission('system:ward:query')")
    public CommonResult<PageResult<WardPageItemReqVO>> getWardPage(@Valid WardPageReqVO pageVO) {
        PageResult<WardDO> pageResult = wardService.getWardPage(pageVO);

        // 获得拼接需要的数据
        Collection<Long> deptIds = convertList(pageResult.getList(), WardDO::getDeptId);
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(deptIds);
        // 拼接结果返回
        List<WardPageItemReqVO> userList = new ArrayList<>(pageResult.getList().size());
        pageResult.getList().forEach(ward -> {
            WardPageItemReqVO respVO = WardConvert.INSTANCE.convert01(ward);
            respVO.setDept(WardConvert.INSTANCE.convert(deptMap.get(ward.getDeptId())));
            userList.add(respVO);
        });
        return success(new PageResult<>(userList, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统病区 Excel")
    @PreAuthorize("@ss.hasPermission('system:ward:export')")
    @OperateLog(type = EXPORT)
    public void exportWardExcel(@Valid WardExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WardDO> list = wardService.getWardList(exportReqVO);
        // 导出 Excel
        List<WardExcelVO> datas = WardConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "系统病区.xls", "数据", WardExcelVO.class, datas);
    }

}
